//
//  GYMTabBar.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/16.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "GYMTabBar.h"
//#import "publishViewController.h"
#import "GYMVerticalButton.h"
#import "publishCenterViewController.h"
@interface GYMTabBar()
/**发布按钮 */
@property (nonatomic, weak) UIButton *publishButton;
@end

@implementation GYMTabBar

// 通过代码重写控件 (不能在这里设置frame)
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        GYMVerticalButton *publishButton = [[GYMVerticalButton alloc]init];
        [publishButton setImage:[UIImage imageNamed:@"main_publish"] forState:UIControlStateNormal];
//        [publishButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        [publishButton setTitle:@"发布" forState:UIControlStateNormal];
        [publishButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [publishButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        publishButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [publishButton addTarget:self action:@selector(publishButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self insertSubview:publishButton aboveSubview:self];
        self.publishButton = publishButton;
    }
    return self;
}


- (void)publishButtonClick{
    
    publishCenterViewController *vc = [[publishCenterViewController alloc]init];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    //修改背景颜色
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    bgView.backgroundColor = [UIColor whiteColor];
    [self insertSubview:bgView atIndex:0];
    self.opaque = YES;

    
    // 标记按钮是否已经添加过监听器
    static BOOL added = NO;
    
    // 设置发布按钮的frame
    self.publishButton.bounds = CGRectMake(0, 0, 50, 60);
    [self bringSubviewToFront:self.publishButton];
    
    self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.45);
    
    self.publishButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    self.publishButton.adjustsImageWhenHighlighted=NO;
    
    // 设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = self.frame.size.width / 5;
    CGFloat buttonH = self.frame.size.height;
    NSInteger index = 0;
    for (UIControl *btn in self.subviews) {
        // 判断是不是UIBarButton
        if (![btn isKindOfClass:[UIControl class]] || btn == self.publishButton) {
            continue;
        }
        // 计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        btn.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        // 增加索引
        index++;
        if (added == NO) {
            // 监听按钮点击
            // 监听按钮点击
            [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    added = YES;
}
- (void)buttonClick
{
    // 发出一个通知
//    [[NSNotificationCenter defaultCenter] postNotificationName:XMGTabBarDidSelectNotification object:nil userInfo:nil];
    NSLog(@"buttonClick");
}

@end
